(function () {

    angular
        .module('accommodation')
        .controller('AccommodationController', [
            'accommodationService', '$mdBottomSheet', '$q', '$mdToast',
            AccommodationController
        ]);

    /**
     * Accommodation Controller for the Angular Material Starter App
     * @param $scope
     * @param $mdSidenav
     * @param avatarsService
     * @constructor
     */
    function AccommodationController(accommodationService, $mdBottomSheet, $q, $mdToast) {
        var self = this;
        this.showCustomToast = showCustomToast;
        var last = {
            bottom: false,
            top: true,
            left: false,
            right: true
        };

        this.toastPosition = angular.extend({},last);
        self.content = {};
        self.showContactOptions = showContactOptions;

        accommodationService
            .loadContent()
            .then(function (content) {
                self.content = content;
            });

        function getToastPosition(){
            sanitizePosition();

            return Object.keys(self.toastPosition)
                .filter(function(pos) { return self.toastPosition[pos]; })
                .join(' ');
        }
        function sanitizePosition() {
            var current = self.toastPosition;

            if ( current.bottom && last.top ) current.top = false;
            if ( current.top && last.bottom ) current.bottom = false;
            if ( current.right && last.left ) current.left = false;
            if ( current.left && last.right ) current.right = false;

            last = angular.extend({},current);
        }
        function showCustomToast() {
            var pinTo = getToastPosition();

            $mdToast.show(
                $mdToast.simple()
                    .textContent('This will be available in the next release, stay tuned!')
                    //.position(pinTo)
                    .hideDelay(3000)
            );
        }

        /**
         * Show the bottom sheet
         */
        function showContactOptions($event) {
            var user = self.selected;

            return $mdBottomSheet.show({
                templateUrl: './src/pages/accommodation/view/share.html',
                controller: ['$mdBottomSheet', ContactPanelController],
                controllerAs: "cp",
                bindToController: true,
                targetEvent: $event
            }).then(function (clickedItem) {
                clickedItem && $log.debug(clickedItem.name + ' clicked!');
            });

            /**
             * Bottom Sheet controller for the Avatar Actions
             */
            function ContactPanelController($mdBottomSheet) {
                this.user = user;
                this.actions = [
                    {name: 'Phone', icon: 'phone', icon_url: 'assets/svg/phone.svg'},
                    {name: 'Twitter', icon: 'twitter', icon_url: 'assets/svg/twitter.svg'},
                    {name: 'Google+', icon: 'google_plus', icon_url: 'assets/svg/google_plus.svg'},
                    {name: 'Hangout', icon: 'hangouts', icon_url: 'assets/svg/hangouts.svg'}
                ];
                this.submitContact = function (action) {
                    $mdBottomSheet.hide(action);
                };
            }
        }
    }
})();
